package com.my.ads.lib.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.myadlib.R;
import com.my.ads.lib.AdSummary;
import com.my.ads.lib.Config;

/**
 * Created by xuanyang on 2018/3/7.
 */

public class ConnectionActivity extends BaseActivity {


    private ImageView img_off, img_ad;
    private RelativeLayout relative_advertisement;
//    private RelativeLayout notice_banner;
    private TextView text_number, text_introduce;

//    public List<PhoneInfo> getPhoneNumberFromMobile(Context context) {
//        List<PhoneInfo> phone_list = new ArrayList<>();
//        try {
//            String[] cols = {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
//            Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
//                    cols, null, null, null);
//            for (int i = 0; i < cursor.getCount(); i++) {
//                cursor.moveToPosition(i);
//                // 取得联系人名字
//                int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
//                int numberFieldColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
//                String name = cursor.getString(nameFieldColumnIndex);
//                String number = cursor.getString(numberFieldColumnIndex);
//                PhoneInfo phoneInfo = new PhoneInfo(name, number);
//                phone_list.add(phoneInfo);
//            }
//            cursor.close();//使用完后一定要将cursor关闭，不然会造成内存泄露等问题
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return phone_list;
//    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_connection);

        initViews();

//        List<PhoneInfo> list = getPhoneNumberFromMobile(this);


        try {
            AdSummary.getInstance().showNativeAd(ConnectionActivity.this, relative_advertisement, false);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        try {
//            AdSummary.getInstance().showBannerAd(notice_banner);
//        } catch (Exception e) {
//
//        }

        if (getIntent() != null) {
            String text = getIntent().getStringExtra("number") + " please remark!";
            text_introduce.setText(text);
            text_number.setText(getIntent().getStringExtra("number"));
        }

        img_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show();
                finish();
            }
        });

        img_ad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intents = new Intent(ConnectionActivity.this, GiftActivity.class);
                startActivity(intents);
                show();
            }
        });


    }

    private void show() {
        AdSummary.getInstance().showInterstitialAd(this, "");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        show();
        finish();
    }

    @Override
    public void onResume() {
        super.onResume();

        SharedPreferences sp_1 = getSharedPreferences(Config.NativeAdTarget, Context.MODE_PRIVATE);
        Long show_time = sp_1.getLong(Config.NativeAdTimeShow, 0L);
        if (show_time != 0L && ((System.currentTimeMillis() - show_time) > Config.UpdateNativeAd * 1000)) {
            try {
                AdSummary.getInstance().showNativeAd(ConnectionActivity.this, relative_advertisement, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
//            try {
//                AdSummary.getInstance().showBannerAd(notice_banner);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
            // 展示时间提交
        }
    }


    private void initViews() {
        img_off = (ImageView) findViewById(R.id.img_off);
        img_ad = (ImageView) findViewById(R.id.img_ad);
        relative_advertisement = (RelativeLayout) findViewById(R.id.relative_advertisement);
//        notice_banner = (RelativeLayout) findViewById(R.id.notice_banner);
        text_introduce = (TextView) findViewById(R.id.text_introduce);
        text_number = (TextView) findViewById(R.id.text_number);
    }


}
